﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace LitTools
{
    /// <summary>
    /// 时间工具
    /// 时间戳
    /// 延迟器
    /// 计时器
    /// </summary>
    public class TimeUtility : MonoSingleton<TimeUtility>
    {
        private Dictionary<string, float> timeStampDict;
        public override void Init()
        {
            base.Init();
            timeStampDict = new Dictionary<string, float>();
        }

        #region 时间戳相关
        /// <summary>
        /// 添加时间戳
        /// </summary>
        /// <param name="key"></param>
        public void AddTimeStamp(string key)
        {
            timeStampDict.Add(key, Time.realtimeSinceStartup);
            Debug.Log($"TimeManager register timeStamp key:{key} time{Time.realtimeSinceStartup}");
        }

        /// <summary>
        /// 通过key获取时间戳
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public float GetTimeStampByKey(string key)
        {
            if (!timeStampDict.ContainsKey(key))
            {
                Debug.Log($"TimeManager timeStamp with key:{key} not exist");
                return 0;
            }
            return timeStampDict[key];
        }
        #endregion

        #region 延迟相关
        /// <summary>
        /// 帧延迟
        /// </summary>
        /// <param name="frame"></param>
        /// <param name="delyAct"></param>
        public void SetFrameDely(int frame, Action delyAct)
        {
            if (frame <= 0)
            {
                delyAct?.Invoke();
                return;
            }
            StartCoroutine(FrameDelyCor(frame, delyAct));
        }
        private IEnumerator FrameDelyCor(int frame, Action delyAct)
        {
            int startFrame = -1;
            while (startFrame < frame)
            {
                startFrame++;
                yield return new WaitForEndOfFrame();
            }
            delyAct?.Invoke();
        }

        /// <summary>
        /// 时间延迟
        /// </summary>
        /// <param name="time"></param>
        /// <param name="delyAct"></param>
        public void SetTimeDely(float time, Action delyAct)
        {
            if (time <= 0)
            {
                delyAct?.Invoke();
                return;
            }
            StartCoroutine(TimeDelyCor(time, delyAct));
        }
        private IEnumerator TimeDelyCor(float time, Action delyAct)
        {
            yield return new WaitForSeconds(time);
            delyAct?.Invoke();
        }
        #endregion

        #region 计时器相关

        #endregion
    }

    /// <summary>
    /// 秒间隔计时器
    /// </summary>
    public class Timer
    {
        public float interval;
        public int loopCount;
        public int curLoop;
        public event Action intervalAct;
        public event Action finishAct;
    }
    /// <summary>
    /// 帧间隔计时器
    /// </summary>
    public class Framer
    {
        public int interval;
        public int loopCount;
        public int curLoop;
        public event Action intervalAct;
        public event Action finishAct;
    }
}